<?php
class Pageley_Domain_Validator_Type extends Pageley_Domain_Validator_Abstract {
	
	private $type;
	private $invalidTypeMessage = "error.validation.invalidType";
	
	public function __construct($type) {
		$this->type = $type;
	}
	
	public function validate($value, $propertyName, $object) {
		$errors = new Pageley_Util_ArrayMap();
		if($value !== null && $value !== "") {
            $invalidType = $this->rejectIfFalse(is_numeric($value) || ($value instanceof Pageley_Domain_Entity && $value->getType()->isAssignableTo($this->type)), 
    			$this->invalidTypeMessage, array("{field." . $propertyName . "}", $value, $value instanceof Pageley_Domain_Entity ? $value->getType() : "{error.notAnEntity}", $this->type));
    		if($invalidType) {
    			$errors->put($propertyName, $invalidType);
    		}
        }
		return $errors;
	}
	
	public function setInvalidTypeMessage($invalidTypeMessage) {
		$this->invalidTypeMessage = $invalidTypeMessage;
	}
	
	
}